// @ts-ignore
import { createRouter, RouteRecordRaw,createWebHistory } from 'vue-router'
// @ts-ignore
import RouterBeforeEach from '@/utils/router/router.beforeEach'
//@ts-ignore
import { getStorage } from '@/utils/storage'
//@ts-ignore
import request from '@/utils/request'
const modules = import.meta.glob('../views/**/index.vue')
const configKeyList = import.meta.glob('../views/**/index.config.ts')
let routesArrayList:Array<RouteRecordRaw> = [];
Object.keys(modules).forEach(async (item:string)=>{
    let path = item.replace("../views","").replace("/index.vue","")
    let configKey = item.replace("index.vue","index.config.ts");
    routesArrayList.push({
        path,
        name:path,
        meta:{
            config:configKeyList[configKey]
        },
        component:modules[item]
    })
})
const routes: Array<RouteRecordRaw> = routesArrayList
const router = createRouter({
    history: createWebHistory(),
    routes
})
//@ts-ignore
router.beforeEach(async (before:any,after:any,next)=>{
    //@ts-ignore
    let res = await before.meta.config()
    let config = res.default;
    if (config.isWhite){
        next();
    }else{
        if (getStorage("token")){
            let res = await request.post("/App/api/isPromoterName")
            if (config.isRealAuth === true){//说明实名页面
                if (res.status == 200){
                    if (before.query && before.query.redirect){
                        next(before.query.redirect)
                    }else{
                        next()
                    }
                }else{
                    next()
                }
            }else{
                if (res.status == 100){//未实名
                    next("/realAuth")
                }else if(res.status == 200){
                    next()
                }else{
                    next("/login");
                }
            }
        }else{
            next("/login");
        }
    }
    RouterBeforeEach.renderDocumentTitle(config);
})

export default router
